package com.sita.bike.ui.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.sita.bike.R;
import com.sita.bike.ui.activity.DataAnalysisActivity;
import com.sita.bike.ui.adapter.DayGatherAdapter;
import com.sita.tboard.ui.event.DateSelectedChangeEvent;

import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class DayGatherFragment extends Fragment {
    public static DayGatherFragment instance;
    @Bind(R.id.recyclerHorizontalView)
    public RecyclerView recyclerView;
    private int recyclerViewWidth;
    private List dataset = null;

    public static DayGatherFragment getInstance() {
        instance = new DayGatherFragment();
        return instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_day_gather, container, false);
        ButterKnife.bind(this, view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        dataset = changeDay();

        //获取recyclerView 的宽度
        ViewTreeObserver vto = recyclerView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                recyclerViewWidth = recyclerView.getWidth();
                final DayGatherAdapter adapter = new DayGatherAdapter(dataset, recyclerViewWidth);
                adapter.setOnItemClickListener(new DayGatherAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        RecyclerView.LayoutManager mLayoutManager = recyclerView.getLayoutManager();
                        ((LinearLayoutManager) mLayoutManager).scrollToPositionWithOffset(position - 3, 0);
                        DataAnalysisActivity.getInstance().setCurrentDay(Integer.valueOf(Integer.valueOf(position - 2)));
                        adapter.setSelectItem(position);
                        adapter.notifyDataSetChanged();
                        EventBus.getDefault().post(new DateSelectedChangeEvent("DayGatherFragmennt"));
                    }
                });


                recyclerView.setAdapter(adapter);
                //获得现在的日，然后移动到该日
                int day = DataAnalysisActivity.getSystemDay();//从1开始
                recyclerView.scrollToPosition(day-1);
                adapter.setSelectItem(day + 2);
                adapter.notifyDataSetChanged();
            }
        });

        return view;
    }

    public List changeDay() {
        int year = DataAnalysisActivity.getSystemYear();
        int month = DataAnalysisActivity.getSystemMonth() + 1;
        List list = null;
        if ((year % 400 == 0) || ((year % 4) == 0 && (year % 100) != 0)) {
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if (list == null) {
                        list = new LinkedList();
                    }
                    if (list.size() > 0) {
                        list.clear();
                    }
                  //31天
                    for (int t = 0; t < 37; t++) {
                        if (t < 3 || t > 33) {
                            list.add("");
                        } else {
                            list.add("" + (t - 2));
                        }
                    }
                    return list;
//30天
                case 4:
                case 6:
                case 9:
                case 11:
                    if (list == null) {
                        list = new LinkedList();
                    }
                    if (list.size() > 0) {
                        list.clear();
                    }
                    for (int t = 0; t < 36; t++) {
                        if (t < 3 || t > 32) {
                            list.add("");
                        } else {
                            list.add("" + (t - 2));
                        }
                    }
                    return list;
//29天
                case 2:
                    if (list == null) {
                        list = new LinkedList();
                    }
                    if (list.size() > 0) {
                        list.clear();
                    }

                    for (int t = 0; t < 35; t++) {
                        if (t < 3 || t > 31) {
                            list.add("");
                        } else {
                            list.add("" + (t - 2));
                        }
                    }
                    return list;
            }


        } else {

            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if (list == null) {
                        list = new LinkedList();
                    }
                    if (list.size() > 0) {
                        list.clear();
                    }
                    for (int t = 0; t < 37; t++) {
                        if (t < 3 || t > 33) {
                            list.add("");
                        } else {
                            list.add("" + (t - 2));
                        }
                    }
                    return list;

                case 4:
                case 6:
                case 9:
                case 11:
                    if (list == null) {
                        list = new LinkedList();
                    }
                    if (list.size() > 0) {
                        list.clear();
                    }
                    for (int t = 0; t < 36; t++) {
                        if (t < 3 || t > 32) {
                            list.add("");
                        } else {
                            list.add("" + (t - 2));
                        }
                    }
                    return list;

                case 2:
                    if (list == null) {
                        list = new LinkedList();
                    }
                    if (list.size() > 0) {
                        list.clear();
                    }
                    for (int t = 0; t < 34; t++) {
                        if (t < 3 || t > 30) {
                            list.add("");
                        } else {
                            list.add("" + (t - 2));
                        }
                    }
                    return list;
            }
        }

        return null;
    }
}
